package com.fastloan.safe.mgt.sys.service.impl;

import com.fastloan.safe.mgt.sys.dao.CmChinaCityMapper;
import com.fastloan.safe.mgt.sys.model.CmChinaCity;
import com.fastloan.safe.mgt.sys.model.vo.AddressFiled;
import com.fastloan.safe.mgt.sys.model.vo.CmChinaCityVo;
import com.fastloan.safe.mgt.sys.service.CmChinaCityService;
import com.kawins.base.BaseMapper;
import com.kawins.base.baseabs.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author rankai
 *         createTime 2017-11-2017/11/8 10:28
 */
@Service
public class CmChinaCityServiceImpl extends BaseServiceImpl<CmChinaCity> implements CmChinaCityService {

    @Autowired
    private CmChinaCityMapper chinaCityMapper;

    @Override
    protected BaseMapper<CmChinaCity> buildDao() {
        return chinaCityMapper;
    }

    @Override
    public Integer selectIdByAdd(AddressFiled addressFiled) {
        CmChinaCity cmChinaCity = new CmChinaCity();
        cmChinaCity.setName(addressFiled.getProvince());
        CmChinaCity province = selectOne(cmChinaCity);
        if (province == null) {
            log.warn("没有匹配到传入的省级地址,返回0 >> {}", addressFiled);
            return 0;
        } else if (addressFiled.getCity() != null) {
            cmChinaCity.setName(addressFiled.getCity());
            cmChinaCity.setPid(province.getId());
            CmChinaCity city = selectOne(cmChinaCity);
            if (city == null) {
                log.warn("没有匹配到传入的市级地址,返回省Id >> {}", addressFiled);
                return province.getId();
            } else if (addressFiled.getArea() != null) {
                cmChinaCity.setName(addressFiled.getArea());
                cmChinaCity.setPid(city.getId());
                CmChinaCity area = selectOne(cmChinaCity);
                if (area == null) {
                    log.warn("没有匹配到传入的区/县级地址,返回市Id >> {}", addressFiled);
                    return city.getId();
                } else {
                    log.debug("匹配到区/县级地址 >> {}", addressFiled);
                    return area.getId();
                }
            } else {
                log.debug("匹配到市级地址 >> {}", addressFiled);
                return city.getId();
            }
        } else {
            log.debug("匹配到省级地址 >> {}", addressFiled);
            return province.getId();
        }
    }

    @Override
    public AddressFiled selectAllIdByAdd(AddressFiled addressFiled) {
        CmChinaCity cmChinaCity = new CmChinaCity();
        cmChinaCity.setName(addressFiled.getProvince());
        CmChinaCity province = selectOne(cmChinaCity);
        if (province != null) {
            addressFiled.setProvinceId(province.getId());
            cmChinaCity.setName(addressFiled.getCity());
            cmChinaCity.setPid(province.getId());
            CmChinaCity city = selectOne(cmChinaCity);
            if (city != null) {
                addressFiled.setCityId(city.getId());
                cmChinaCity.setName(addressFiled.getArea());
                cmChinaCity.setPid(city.getId());
                CmChinaCity area = selectOne(cmChinaCity);
                if (area != null) {
                    addressFiled.setAreaId(area.getId());
                } else {
                    log.warn("没有匹配到传入的省级地址,返回0 >> {}", addressFiled);
                }
            } else {
                log.warn("没有匹配到传入的市级地址,返回省Id >> {}", addressFiled);
            }
        } else {
            log.warn("没有匹配到传入的区/县级地址,返回市Id >> {}", addressFiled);
        }
        return addressFiled;
    }

    @Override
    public CmChinaCityVo selectIdByTree(Integer areaId) {
        CmChinaCityVo CmChinaCityVoOne = chinaCityMapper.selectByIdForVo(areaId);
        Integer pid = CmChinaCityVoOne.getPid();
        if (pid != 0) {
            CmChinaCityVo cmChinaCityVoTwo = chinaCityMapper.selectByIdForVo(pid);
            cmChinaCityVoTwo.setCmChinaCityVo(CmChinaCityVoOne);
            pid = cmChinaCityVoTwo.getPid();
            if (pid != 0) {
                CmChinaCityVo cmChinaCityVoTree = chinaCityMapper.selectByIdForVo(pid);
                cmChinaCityVoTree.setCmChinaCityVo(cmChinaCityVoTwo);
                return cmChinaCityVoTree;
            } else {
                return cmChinaCityVoTwo;
            }
        } else {
            return CmChinaCityVoOne;
        }
    }
}
